package com.CloudDream.Listener;

import com.CloudDream.Gui.MYInventoryHolder;
import com.CloudDream.Inventory.InventoryItem;
import com.CloudDream.ItemUtil;
import com.CloudDream.MoonCake;
import com.CloudDream.Util.Lore;
import com.CloudDream.config.Config;
import com.handy.lib.annotation.HandyListener;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@HandyListener
public class CookGuiEvent implements Listener {

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
        int[] Slots = {10, 11, 12, 19, 20, 21, 24, 28, 29, 30};
        int[] Slots1 = {10, 11, 12, 19, 21, 28, 29, 30};
        List<Integer> Slots2 = Arrays.asList(10, 11, 12, 19, 20, 21, 28, 29, 30);
        List<Integer> MaterialSlot = Arrays.asList(7, 8, 9, 10, 11, 12, 13);

        Inventory inv = e.getInventory();
        Player p = (Player) e.getWhoClicked();

        if (inv.getHolder() == null) return;
        if (!(inv.getHolder() instanceof MYInventoryHolder)) return;
        if (((MYInventoryHolder) inv.getHolder()).getType() != 0) return;
        int slot = e.getRawSlot();
        if (e.getClick().equals(ClickType.DOUBLE_CLICK)) {
            if (slot > 44 && slot < 90) {
                e.setCancelled(true);
                return;
            }
        }
        if (slot >= 0 && slot <= 44) {
            List<Integer> Inventoryslot = new ArrayList<>();
            for (int s : Slots) {
                Inventoryslot.add(s);
            }
            if (!Inventoryslot.contains(slot)) {
                e.setCancelled(true);
            }
        }

        if (slot == 40) {
            if (inv.getItem(24) != null) {
                p.sendMessage(MoonCake.prefix + "§c貌似有啥东西放在了输出槽里?这样会导致月饼无法制作出来");
                return;
            }
            for (int table : Slots1) {
                if(inv.getItem(table) == null && inv.getItem(20) == null){
                    p.sendMessage(MoonCake.prefix + "§c啥也没放入，不能合成月饼哦");
                    return;
                }
                if (inv.getItem(table) != null && (!Lore.Check(inv.getItem(table), InventoryItem.getMaterial(0)))) {
                    p.sendMessage(MoonCake.prefix + "§c请取走除§a§l月饼皮§c以外的物品");
                    return;
                }
                for (int s1 : MaterialSlot) {
                    if (inv.getItem(20) != null && inv.getItem(table) == null) {
                        p.sendMessage(MoonCake.prefix + "§c请放入§a§l月饼皮§c再放入馅料");
                        return;
                    }
                    if (inv.getItem(table).getItemMeta().getDisplayName().equals("§a月饼皮") && inv.getItem(20) == null) {
                        p.sendMessage(MoonCake.prefix + "§c请放入用于制作月饼的基础馅料");
                        return;
                    }
                    if (inv.getItem(20) != null && !Lore.Check(inv.getItem(20), InventoryItem.getMaterial(s1))) {
                        p.sendMessage(MoonCake.prefix + "§c请不要放置除§b§l月饼馅料§c以外的物品");
                        return;
                    }
                }
            }
            List<String> name = Arrays.asList("§a莲蓉", "§a五仁", "§a蛋黄", "§a冰糖", "§a玫瑰馅", "§a果酱", "§a肉松");
            String player = p.getName() + ".MoonCake.";

            if (ItemUtil.isItem(inv, Slots2, 20, "§a月饼皮", name)) {

                int MoonCakeID = name.indexOf(inv.getItem(20).getItemMeta().getDisplayName());
                inv.setItem(24, InventoryItem.getMoonCake(MoonCakeID));
                String ItemName = inv.getItem(24).getItemMeta().getDisplayName();

                if (Config.pd.get(player + MoonCakeID + ".amount") != null) {
                    Config.pd.set(player + MoonCakeID + ".amount", (int) Config.pd.get(player + MoonCakeID + ".amount") + 1);
                } else {
                    Config.pd.set(player + MoonCakeID + ".name", ItemName);
                    Config.pd.set(player + MoonCakeID + ".amount", Objects.requireNonNull(inv.getItem(24)).getAmount());
                }

                for (int s2 : Slots2) {
                    if (Objects.requireNonNull(inv.getItem(s2)).getAmount() == 1) {
                        inv.setItem(s2, null);
                    }
                }
                Config.savepd();
                p.sendMessage(MoonCake.prefix + "§a你已成功制作了一款月饼，请尽情享用！祝你中秋节快乐~");
                p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_YES, 1, 1);
            }
        }

    }


    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e) {
        Inventory inv = e.getInventory();
        if (inv == null || inv.getHolder() == null) return;
        if (!(inv.getHolder() instanceof MYInventoryHolder)) return;
        if (((MYInventoryHolder) inv.getHolder()).getType() != 0) return;
        Player p = (Player) e.getPlayer();
        Location pos = p.getLocation();
        int[] Slots = {10, 11, 12, 19, 20, 21, 24, 28, 29, 30};

        for (int slot : Slots) {
            if (inv.getItem(slot) != null) {
                if (slot != 24) {
                    p.getInventory().addItem(inv.getItem(slot));
                }
                if (inv.getItem(24) != null) {
                    p.getInventory().addItem(inv.getItem(24));
                }
            }
        }
    }
}
